package pers.zwy.DMSystem.repairManagement.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import pers.zwy.DMSystem.repairManagement.dao.RepairDao;
import pers.zwy.DMSystem.repairManagement.domain.Repair;
import pers.zwy.DMSystem.util.dao.impl.BaseDaoImpl;

public class RepairDaoImpl extends BaseDaoImpl implements RepairDao {

	public String getInsertSql() {
		return "insert into t_repair (title,content,stu_id,dormitory_id,state,date)values(?,?,?,?,?,?)";
	}

	public void setInsertSql(Object data, PreparedStatement pstm) throws Exception {
		Repair repair = (Repair) data;
		pstm.setString(1, repair.getTitle());
		pstm.setString(2, repair.getContent());
		pstm.setString(3, repair.getStu_id());
		pstm.setInt(4, repair.getDormitory().getId());
		pstm.setString(5, repair.getState());
		pstm.setTimestamp(6, repair.getDate());
	}

	public String getDeleteSql() {
		return "delete from t_repair where id=?";
	}

	public void setDeleteSql(int id, PreparedStatement pstm) throws Exception {
		pstm.setInt(1, id);
	}

	public String getUpdateSql() {
		return "update t_repair set title=?,content=?,stu_id=?,dormitory_id=?,state=? where id=?";
	}

	public void setUpdateSql(Object data, PreparedStatement pstm) throws Exception {
		Repair repair = (Repair) data;
		pstm.setString(1, repair.getTitle());
		pstm.setString(2, repair.getContent());
		pstm.setString(3, repair.getStu_id());
		pstm.setInt(4, repair.getDormitory().getId());
		pstm.setString(5, repair.getState());
		pstm.setInt(6, repair.getId());
	}

	public String getFindByIdSql() {
		return "select *from t_repair where id=?";
	}

	public void setFindByIdSql(int id, PreparedStatement pstm) throws Exception {
		pstm.setInt(1, id);
	}

	public String getFindAllSql() {
		return "select *from t_repair";
	}

	public String getFindByConditionSql(Object data) throws Exception {
		Repair repair = (Repair)data;
		StringBuilder sql = new StringBuilder("select * from t_repair where 1=1");
		if(repair.getContent()!=null) {
			sql.append(" and content='"+repair.getContent()+"'");
		}
		if(repair.getDormitory()!=null) {
			sql.append(" and dorminitory_id="+repair.getDormitory().getId());
		}
		if(repair.getId()!=null) {
			sql.append(" and id="+repair.getId());
		}
		if(repair.getState()!=null) {
			sql.append(" and state='"+repair.getState()+"'");
		}
		if(repair.getStu_id()!=null) {
			sql.append(" and stu_id='"+repair.getStu_id()+"'");
		}
		if(repair.getTitle()!=null) {
			sql.append(" and title="+repair.getTitle());
		}
		return new String(sql);
	}

	public Object rsToObject(ResultSet rs) throws Exception {
		Repair repair = new Repair();
		repair.setId(rs.getInt("id"));
		repair.setTitle(rs.getString("title"));
		repair.setContent(rs.getString("content"));
		repair.setState(rs.getString("state"));
		repair.setStu_id(rs.getString("stu_id"));
		repair.getDormitory().setId(rs.getInt("dormitory_id"));
		repair.setDate(rs.getTimestamp("date"));
		return repair;
	}

}
